 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
 // Jad home page: http://www.kpdus.com/jad.html
 // Decompiler options: packimports(3) 
 // Source File Name:   JBTreeNode.java

 package com.jb.web.bean;

 import com.jb.faces.component.NamingContainer;
 import com.jb.faces.context.FacesContext;
 import com.jb.framework.PersistentContext;
 import com.jb.util.ClassUtil;
 import com.jb.web.application.EventManager;
 import com.jb.web.bean.event.SelectionListner;
 import com.jb.web.bean.event.UIEvent;
 import com.jb.web.bean.event.UIEventHandler;
 import com.jb.web.bean.inspector.FilePropertyEditor;
 import com.jb.web.context.WebContext;
 import com.jb.web.renderer.NodeRenderer;
 import java.util.*;
 import javax.servlet.http.HttpServletRequest;
 import javax.swing.tree.TreeModel;
 import javax.swing.tree.TreeNode;

 // Referenced classes of package com.jb.web.bean:
 //            Link, BaseComponent

 public class JBTreeNode extends Link
     implements NamingContainer
 {

     private void $init$()
     {
         destination = "#";
         closeListners = new Vector();
         openListners = new Vector();
         selectListners = new Vector();
         openOne = false;
         nodeState = 1;
     }

     public JBTreeNode()
     {
         $init$();
     }

     public JBTreeNode getChild(int loc)
     {
         return (JBTreeNode)getComponentAt(loc);
     }

     public void _onSelected()
     {
         if(isRoot())
             if(nodeState == 0)
             {
                 nodeState = 1;
             } else
             {
                 if(getParentNode() != null && getParentNode().openOne)
                     closeAll();
                 nodeState = 0;
             }
     }

     public boolean isChildAccepatble(BaseComponent component)
     {
         return getClass() == component.getClass();
     }

     public List getChildNodes()
     {
         return getChildrenByType(JBTreeNode.class);
     }

     public void setCloseIcon(String closeIcon)
     {
         this.closeIcon = closeIcon;
     }

     public String getCloseIcon()
     {
         return closeIcon;
     }

     public void setDefault(PersistentContext ctx)
     {
         setDestination("${context.pageName}.xsp");
     }

     public String getDestination()
     {
         return destination;
     }

     public void setIconImage(String iconImage)
     {
         this.iconImage = iconImage;
     }

     public String getIconImage()
     {
         return iconImage;
     }

     public boolean isLast()
     {
         if(getParentNode() == null)
         {
             return true;
         } else
         {
             JBTreeNode parent = getParentNode();
             int i = parent.getChildNodes().size() - 1;
             return this == parent.getChildNodes().get(i);
         }
     }

     public boolean isLeaf()
     {
         Object userObject = getUserObject();
         if(userObject != null && (userObject instanceof TreeNode))
             return ((TreeNode)userObject).isLeaf();
         if(treeModel != null)
             return treeModel.getChildCount(userObject) == 0;
         else
             return getChildCount() == 0 && !isRoot();
     }

     public void setNodeState(int newNodeState)
     {
         nodeState = newNodeState;
     }

     public int getNodeState()
     {
         return nodeState;
     }

     public boolean isOpen()
     {
         return nodeState == 0;
     }

     public void setOpenIcon(String newOpenIcon)
     {
         openIcon = newOpenIcon;
     }

     public String getOpenIcon()
     {
         return openIcon;
     }

     public void setOpenOne(boolean openOne)
     {
         this.openOne = openOne;
     }

     public boolean isOpenOne()
     {
         return openOne;
     }

     public JBTreeNode getParentNode()
     {
         return (getParent() instanceof JBTreeNode) ? (JBTreeNode)getParent() : null;
     }

     public boolean isRoot()
     {
         return getParent() == null || !(getParent() instanceof JBTreeNode);
     }

     public JBTreeNode getRootNode()
     {
         JBTreeNode parent = getParentNode();
         if(parent == null)
             return this;
         else
             return parent.getRootNode();
     }

     public void setSelected(boolean newSelected)
     {
         selected = newSelected;
     }

     public void setSelected(WebContext ctx, boolean newSelected)
     {
         selected = newSelected;
         UIEvent event = new UIEvent(this, ctx.getRequest());
         processSelectLisnters(ctx, event);
     }

     public boolean isSelected()
     {
         return selected;
     }

     public String getSelectedStyle(WebContext ctx)
     {
         return selectedStyle != null && selectedStyle.length() != 0 ? selectedStyle : getParentNode() != null ? getParentNode().getSelectedStyle(ctx) : "jb_tn_txt_selected";
     }

     public String getSelectedStyle()
     {
         return selectedStyle;
     }

     public static JBTreeNode getTree(WebContext ctx, BaseComponent panel)
     {
         try
         {
             JBTreeNode mNode = (JBTreeNode)ClassUtil.createInstance(JBTreeNode.class);
             mNode.setId((new StringBuilder()).append("tree").append(panel.getId()).toString());
             mNode.buildTree(ctx, panel);
             mNode.setDestination("${context.pageName}.xsp");
             JBTreeNode jbtreenode = mNode;
             return jbtreenode;
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
         JBTreeNode jbtreenode1 = null;
         return jbtreenode1;
     }

     public void setOnSelect(String onSelect)
     {
         this.onSelect = onSelect;
     }

     public String getOnSelect()
     {
         return onSelect;
     }

     public JBTreeNode getSelectedNode()
     {
         if(isSelected())
             return this;
         List childNodes = getChildren();
         for(Iterator iter = childNodes.iterator(); iter.hasNext();)
         {
             JBTreeNode child = (JBTreeNode)iter.next();
             JBTreeNode selectedNode = child.getSelectedNode();
             if(selectedNode != null)
                 return selectedNode;
         }

         return null;
     }

     public void setSelectedStyle(String selectedStyle)
     {
         if(selectedStyle == null || selectedStyle.length() == 0)
             this.selectedStyle = null;
         else
             this.selectedStyle = selectedStyle;
     }

     public void setUserObject(Object source)
     {
         userObject = source;
     }

     public Object getUserObject()
     {
         return userObject;
     }

     public String getValue(WebContext ctx)
     {
         return userObject != null ? userObject.toString() : (new StringBuilder()).append("").append(super.getValue()).toString();
     }

     public void addCloseListner(UIEventHandler listner)
     {
         closeListners.add(listner);
     }

     public void addOpenListner(UIEventHandler listner)
     {
         openListners.add(listner);
     }

     public void addSelectListner(SelectionListner listner)
     {
         selectListners.add(listner);
     }

     private void buildTree(WebContext ctx, BaseComponent htmlItem)
     {
         clearChildren();
         List list = htmlItem.getChildren();
         for(int i = 0; i < list.size(); i++)
         {
             BaseComponent item = (BaseComponent)list.get(i);
             String itemName = (new StringBuilder()).append(item.getId()).append(":").append(item.getClass().getName()).toString();
             JBTreeNode mNode = (JBTreeNode)ClassUtil.createInstance(JBTreeNode.class);
             mNode.setDestination("${context.pageName}.xsp");
             mNode.setId(itemName);
             mNode.setUserObject(item);
             addChild(mNode);
             mNode.buildTree(ctx, item);
         }

     }

     public void delete(WebContext ctx)
     {
         if(getChildCount() != 0)
         {
             JBTreeNode child;
             for(Iterator it = getChildren().iterator(); it.hasNext(); child.delete(ctx))
                 child = (JBTreeNode)it.next();

             clearChildren();
         }
         super.delete(ctx);
     }

     public void processUpdates(FacesContext facesCtx)
     {
         WebContext ctx = (WebContext)facesCtx;
         if(isMyEvent(ctx))
         {
             if("_oc".equals(ctx.getRequest().getParameter("action")))
                 toggleState(ctx);
             if(getRootNode() != null && getRootNode().getSelectedNode() != null)
                 getRootNode().getSelectedNode().setSelected(false);
             setSelected(ctx, true);
         }
         super.processUpdates(ctx);
     }

     public void removeCloseListner(UIEventHandler listner)
     {
         closeListners.remove(listner);
     }

     public void removeOpenListner(UIEventHandler listner)
     {
         openListners.remove(listner);
     }

     public void removeSelectListner(SelectionListner listner)
     {
         selectListners.remove(listner);
     }

     public void toggleState(FacesContext facesCtx)
     {
         WebContext ctx = (WebContext)facesCtx;
         UIEvent event = new UIEvent(this, ctx.getRequest());
         if(nodeState == 0)
         {
             nodeState = 1;
             processCloseLisnters(ctx, event);
         } else
         {
             if(getParentNode() != null && getParentNode().openOne)
                 closeAll();
             nodeState = 0;
             processOpenLisnters(ctx, event);
         }
     }

     protected void processSelectLisnters(WebContext ctx, UIEvent event)
     {
         SelectionListner listner;
         for(Iterator iter = selectListners.iterator(); iter.hasNext(); listner.onSelect(ctx, event))
             listner = (SelectionListner)iter.next();

         if(onSelect != null && onSelect.length() != 0)
             EventManager.getInstance().handleEvent(ctx, this, onSelect);
         if(getParentNode() != null)
             getParentNode().processSelectLisnters(ctx, event);
     }

     private void closeAll()
     {
         for(Iterator iter = getParentNode().getChildNodes().iterator(); iter.hasNext();)
         {
             JBTreeNode node = (JBTreeNode)iter.next();
             node.nodeState = 1;
         }

     }

     private void processCloseLisnters(WebContext ctx, UIEvent event)
     {
         UIEventHandler handler;
         for(Iterator iter = closeListners.iterator(); iter.hasNext(); handler.handleEvent(ctx, event))
             handler = (UIEventHandler)iter.next();

         if(onClose != null && onClose.length() != 0)
             EventManager.getInstance().handleEvent(ctx, this, onClose);
     }

     private void processOpenLisnters(WebContext ctx, UIEvent event)
     {
         UIEventHandler handler;
         for(Iterator iter = openListners.iterator(); iter.hasNext(); handler.handleEvent(ctx, event))
             handler = (UIEventHandler)iter.next();

         if(onOpen != null && onOpen.length() != 0)
             EventManager.getInstance().handleEvent(ctx, this, onOpen);
     }

     public void setDestination(String destination)
     {
         this.destination = destination;
     }

     public String get_destination()
     {
         return destination;
     }

     public void setTreeModel(TreeModel param)
     {
         treeModel = param;
     }

     public TreeModel getTreeModel()
     {
         return treeModel;
     }

     public void setNodeRenderer(NodeRenderer param)
     {
         nodeRenderer = param;
     }

     public NodeRenderer getNodeRenderer()
     {
         return nodeRenderer;
     }

     static final long serialVersionUID = 0x1e0f3L;
     public static final String SELECT = "select";
     public static final String ACTION = "action";
     public static final int OPEN = 0;
     public static final int CLOSE = 1;
     private transient Object userObject;
     private String destination;
     private String closeIcon;
     private String iconImage;
     private String onClose;
     private String onOpen;
     private String onSelect;
     private String openIcon;
     private String selectedStyle;
     private Vector closeListners;
     private Vector openListners;
     private Vector selectListners;
     private boolean openOne;
     private boolean selected;
     private int nodeState;
     private TreeModel treeModel;
     private NodeRenderer nodeRenderer;

     static 
     {
         publish(JBTreeNode.class, "General", "iconImage", new FilePropertyEditor());
         publish(JBTreeNode.class, "Event Handler", "onSelect");
     }
 }
